/****************************************************************
 ** Product  :   HP Subscriber Network Application Policy
 ** Module   :   gr-manager
 ** Date: Apr 10, 2015               
 ** Author: Hua Zhen
 ** (C) Copyright 2015, Hewlett-Packard Company, All Rights Reserved.
 ** This software is the proprietary information of HP, Inc.  
 ** Use is subject to license terms.
 *****************************************************************
 ** Description:   
 **   Stop the DR agent process for specific DB instance.
 **    
 ****************************************************************/
package com.hp.snap.gr.action.impl;

import com.hp.snap.gr.db.voltdb.ControlDRAgent;
import com.hp.snap.gr.exception.ActionException;
import com.hp.snap.gr.model.Database;
import com.hp.snap.gr.model.jaxb.ActionType;
import com.hp.snap.gr.model.jaxb.GeneralActionType;
import com.hp.snap.gr.model.jaxb.ParameterType;
import com.hp.snap.gr.utils.Constants;
import com.hp.snap.gr.utils.GRMConfigManager;
import com.hp.snap.gr.utils.RESTfulUtils;
import com.hp.snap.gr.utils.Utils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.Map;

/**
 * 
 * Stop the DR agent process for specific DB instance;.
 * 
 * There is a parameter can be configured in the action: DB_INSTANCE_NAME.
 * DB_INSTANCE_NAME: The VoltDB DB instance name, such as: spr_db, session_db, lb_db;.
 * 
 * <P>
 * Version: 3.3 <br>
 * Author: Hua Zhen
 * Date: Apr 10, 2015
 * 
 * </P>
 * 
 **/
public class StopDRAgentActionHandler extends AbstractActionHandler {
    private static Logger logger = LoggerFactory.getLogger(StopDRAgentActionHandler.class);

    @Override
    public void execute(ActionType action,
                        Map<String, Object> actionPlanInputs,
                        Map<String, Object> actionPlanOutputs) throws ActionException {
        GeneralActionType actionType = (GeneralActionType) action;
        List<ParameterType> parameterTypes = actionType.getParameter();
        Map<String, String> parameterMap = Utils.contructParamMap(parameterTypes);

        /**
         * get the db instance name, which should be configured in: /SNAP/GR/<DEPLOY_NAME>/VOLTDB/<DB_INSTANCE_NAME>, e.g.
         * /SNAP/GR/Site_A1/VOLTDB/session_db]
         */
        String dbInstName = parameterMap.get("DB_INSTANCE_NAME");
        if (StringUtils.isEmpty(dbInstName)) {
            throw new ActionException("The parameter [DB_INSTANCE_NAME] should be configured!");
        }

        // find out the target currentDatabase profile
        Database currentDatabase = RESTfulUtils.getVoltDBDatabase(dbInstName);

        if (currentDatabase == null) {
            throw new ActionException("The currentDatabase instance is not exist: " + dbInstName);
        }

        String status = currentDatabase.getStatus();

        if (Constants.VDB_STATUS_ONLINE.equals(status)) {
            boolean replica = currentDatabase.isReplica();

            if (!replica) {
                logger.warn("The DB Instance [{}] is not running in replica mode!", dbInstName);
            } else {
                // check DR running status
                boolean dragentStatus = ControlDRAgent.isDRAgentEnabled(currentDatabase.getPort(), false);

                if (dragentStatus) {
                    boolean isDRStopped = ControlDRAgent.stopDRAgent(currentDatabase.getPort());
                    if (!isDRStopped) {
                        throw new ActionException("DR cannot be stopped");
                    }
                    logger.info("The DR agent stopped successfully");
                } else {
                    logger.info("The DR agent has already stopped");
                }
            }
        } else {
            logger.info("The currentDatabase is in status [{}], stop DR agent operation have not been processed!", status);
        }

    }

}
